package com.jlkf.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.common.Constant;
import com.jlkf.dao.AdvertisingManagerMapper;
import com.jlkf.pojo.AdvertisingManager;
import com.jlkf.pojo.SugFeedback;
import com.jlkf.pojo.User;
import com.jlkf.service.AdvertisingManagerService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;
import com.platform.server.util.UUIDUtil;

import Decoder.BASE64Decoder;
@Service
public class AdvertisingManagerServiceImpl implements AdvertisingManagerService{

	@Autowired
	private AdvertisingManagerMapper advertisingManagerMapper;
	
	@Override
	public Object getList(String title,Integer type, Integer pageNo, Integer pageSize) {
		Map<String,Object> map = new HashMap<>();
		map.put("title", title);
		try {
			 PageHelper.startPage(pageNo, pageSize);
			 if(type==1){
				 int[] ct={1,3};
				 map.put("ctype",ct);
			 }else{
				 int[] ct={2,4};
				 map.put("ctype",ct);
			 }
			 List<AdvertisingManager> list = advertisingManagerMapper.getAdvertisingManager(map);
			 PageInfo<AdvertisingManager> info = new PageInfo(list);
			Integer totalPage= info.getPages();
			Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"AdvertisingManager list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object addOrUpdate(String json,HttpServletRequest request) {
		AdvertisingManager am = StringTools.json2Obj(json, AdvertisingManager.class);
		if(am==null){
			return new ReturnData(201,"The parameter list is empty!");
		}
		try {
			if(am.getId()!=null){
				/*if(!StringTools.isEmpty(am.getPicture())){
//					String url=getFileUrl(am.getPicture(),request);
					am.setPicture(url);//头像
					am.setUrl(url);//
				}*/
				int num=advertisingManagerMapper.updateByPrimaryKeySelective(am);
				if(num==0){
					return new ReturnData(202,"fail");
				}
				return new ReturnData(200,"success");
			}else{
				/*if(!StringTools.isEmpty(am.getPicture())){
					String url=getFileUrl(am.getPicture(),request);
					am.setPicture(url);//头像
					am.setUrl(url);//头像
				}*/
				am.setCreateTime(new Date());
				int num=advertisingManagerMapper.insertSelective(am);
				if(num==0){
					return new ReturnData(202,"fail");
				}
				return new ReturnData(200,"success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}
	 //将base64图片 写入文件
		/*public String getFileUrl(String file,HttpServletRequest request){
			 String  dir= "FAB";     
			 String acesP ="";
			  String path = "";
			if(file!=null&&!"".equals(file)){
				 String content = file.split(";base64,")[1];
				 String jp = file.split(";base64,")[0].split("/")[1];
					// 写入磁盘
					BASE64Decoder decoder = new BASE64Decoder();
					try {
						byte[] decodedBytes = decoder.decodeBuffer(content);
						String trueFileName= UUIDUtil.getOrderSnByTime18()+"."+jp;
						 acesP = "http://"+ Constant.SERVER_NAME+":"+request.getServerPort()+"//"+"file"+"//"+dir+"//"+trueFileName;  
						 // 项目在容器中实际发布运行的根路径
				            String realPath =  request.getSession().getServletContext().getRealPath("/");
				            String path2 = realPath.substring(0,realPath.lastIndexOf(System.getProperty("file.separator")));
				            String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator"))))+System.getProperty("file.separator")+"file"+System.getProperty("file.separator")+dir;
				            // 设置存放图片文件的路径
				            path=p+System.getProperty("file.separator")+trueFileName;  
				            File localFile = new File(p+System.getProperty("file.separator"));
				            if(!localFile.exists()){
	                            localFile.mkdirs();
	                        }
						 FileOutputStream out = new FileOutputStream(path);
						out.write(decodedBytes);
						out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
			}
			return acesP;
		}*/
	@Override
	public Object del(Integer id) {
		try {
			int num=advertisingManagerMapper.deleteByPrimaryKey(id);
			if(num==0){
				return new ReturnData(201,"删除失败");
			}
			return new ReturnData(200,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	
}
